package com.bkgy.controller.planking.controller;

import com.bkgy.controller.planking.entity.dto.PlanKingTwoHour;
import com.bkgy.controller.planking.entity.dto.TPdi1870Code;
import com.bkgy.controller.planking.service.IProfit1870JobService;
import com.bkgy.controller.planking.service.ITPdiCodeService;
import com.bkgy.controller.planking.service.PlanKing1870Service;
import com.bkgy.controller.technology.service.ICgl1870ProcessService;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 镀锌外板报表信息
 */
@Tag(name = "镀锌1870产线", description = "镀锌1870产线")
@RestController
@RequiredArgsConstructor
@RequestMapping("/planking")
public class PlanKing1870Controller {
    private final PlanKing1870Service service;
    private final ICgl1870ProcessService cglService;
    private final ITPdiCodeService defectService;
    private final IProfit1870JobService jobService;

    @Operation(summary = "镀锌外板列表", description = "根据条件查询镀锌外板数据")
    @PostMapping("/getPlanKing1870Data")
    public ResultData getPlanKing1870Data(String startTime, String endTime) {
        return service.getPlanKing1870Data(startTime, endTime);
    }

    @Operation(summary = "外板两小时总结", description = "外板两小时总结")
    @PostMapping("/get1870twoHour")
    public ResultData<PlanKingTwoHour> get1870twoHour(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        return service.get1870twoHour(startTime, endTime);
    }

    @Operation(summary = "【外板基础数据】", description = "分离外板生产数据，给外板报表做基础")
    @GetMapping("/splitPlankService")
    @ResponseBody
    public ResultData<String> splitPlankService() {
        cglService.splitPlankService();
        return ResultData.ResultDataSuccess("success");
    }

    @Operation(summary = "【导入外板编号与名称】", description = "外板编号与名称")
    @PostMapping("/importCodeParam")
    @ResponseBody
    public ResultData<String> importCodeParam() {
        ResultData<String> res = service.importCodeParam();
        //刷新加载数据
        List<TPdi1870Code> codeList = defectService.list();
        for (TPdi1870Code code : codeList) {
            CommonConstant.defectCodeMap.put(code.getDefectCode(), code.getDefectName());
        }
        return res;
    }

    @Operation(summary = "【导入外板归户代码与名称】", description = "外板归户与名称")
    @PostMapping("/importGuihuCode")
    @ResponseBody
    public ResultData<String> importGuihuCode() {
        return service.importGuihuCode();
    }

    @Operation(summary = "【构建tb_profit1870表数据】", description = "生产效益手工更新数据 yyyy-MM-dd")
    @GetMapping("/buildTbProfit1870")
    public ResultData buildTbProfit1870(String startDate) {
        return service.buildTbProfit1870(startDate);
    }

    @Operation(summary = "【构建tb_profit1870表数据--立即刷入最新数据】", description = "生产效益手工更新数据")
    @GetMapping("/buildTbProfit1870AtOnce")
    public ResultData buildTbProfit1870AtOnce() {
        jobService.profit1870Job();
        return ResultData.ResultDataSuccess("success");
    }

    /**
     * 补1天数据只用开始时间，格式2025-08-01 08
     * 补区间数据要开始时间和结束时间，格式 2025-08-01    2025-08-15
     */
    @Operation(summary = "【构建tb_Profit1870_res表数据】", description = "构建镀锌预计产量和效益")
    @PostMapping("/buildProfit1870Job")
    @ResponseBody
    public ResultData<String> buildProfit1870Job(String startDate, @RequestParam(required = false) String endDate) {
        if (StringUtils.isNotEmpty(endDate)) {
            Date d1 = DateUtil.getStringToDate("yyyy-MM-dd", startDate);
            Date d2 = DateUtil.getStringToDate("yyyy-MM-dd", endDate);
            long day = (d2.getTime() - d1.getTime()) / (24 * 60 * 60 * 1000);
            Calendar cal = Calendar.getInstance();
            for (int i = 0; i < day; i++) {
                cal.setTime(d1);
                cal.set(Calendar.HOUR_OF_DAY, 8);
                jobService.profit1870ResJob(cal.getTime());
                cal.set(Calendar.HOUR_OF_DAY, 19);
                jobService.profit1870ResJob(cal.getTime());

                cal.add(Calendar.DAY_OF_MONTH, 1);
                d1 = cal.getTime();
            }
        } else {
            Date d1 = DateUtil.getStringToDate("yyyy-MM-dd HH", startDate);
            jobService.profit1870ResJob(d1);
        }
        return ResultData.ResultDataSuccess("success");
    }
}
